package com.example.zx_springboot.service.iml;

import com.example.zx_springboot.entity.ZxRole;
import com.example.zx_springboot.entity.Zxcustomer;
import com.example.zx_springboot.entity.Zxicon;
import com.example.zx_springboot.entity.Zxloss;
import com.example.zx_springboot.mapper.Zx_RoleMapper;
import com.example.zx_springboot.service.ZxRoleService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ZxRoleiml implements ZxRoleService {
    @Autowired
    private Zx_RoleMapper zx_roleMapper;
    @Override
    public PageInfo rolefindByPage(Integer pageNum, Integer pageSize, String role_name) {

        Page<Zxcustomer> page = PageHelper.startPage(pageNum, pageSize);
        //查询出来所有内容，会根据上面设置的分页参数显示
        if (role_name != "") {
            role_name = "%" + role_name + "%";
            List<ZxRole> zxRoles = zx_roleMapper.selectrole(role_name);
            PageInfo<ZxRole> zxrolePageInfo = new PageInfo<>(zxRoles);
            System.out.println("当前页" + zxrolePageInfo.getPageNum());
            System.out.println("总页数" + zxrolePageInfo.getPages());
            System.out.println("页大小" + zxrolePageInfo.getSize());
            System.out.println("总记录数" + zxrolePageInfo.getTotal());
            System.out.println("当前页数据" + zxrolePageInfo.getList());

            return zxrolePageInfo;
        } else {
            List<ZxRole> zxRoles = zx_roleMapper.getall();
            PageInfo<ZxRole> rolePageInfo = new PageInfo<>(zxRoles);
            System.out.println("当前页" + rolePageInfo.getPageNum());
            System.out.println("总页数" + rolePageInfo.getPages());
            System.out.println("页大小" + rolePageInfo.getSize());
            System.out.println("总记录数" + rolePageInfo.getTotal());
            System.out.println("当前页数据" + rolePageInfo.getList());

            return rolePageInfo;
        }
    }

    @Override
    public void updaterole(ZxRole zxRole) {
        zx_roleMapper.upadte(zxRole);
    }

    @Override
    public void deleterole(Integer role_id) {
        zx_roleMapper.delete(role_id);
    }

    @Override
    public void deleteroles(List<Integer> ids) {
        for (Integer role_id : ids) {
            // 进行删除操作，可以调用你的删除方法
            zx_roleMapper.delete(role_id);
        }
    }

    @Override
    public void addrole(ZxRole zxRole) {
        zx_roleMapper.add(zxRole);
    }

    @Override
    public void savemenus(Integer role_id, List<Integer> menuids) {
        zx_roleMapper.deletemenus(role_id);
        Map<String,Integer> paramMap = new HashMap<>();
        paramMap.put("role_id", role_id);

        for(Integer resource_id :menuids){
            paramMap.put("resource_id", resource_id);
            zx_roleMapper.addmenu(paramMap);

        }

    }

    @Override
    public List<Integer> exitmenus(Integer role_id) {
        return zx_roleMapper.exitmenus(role_id);
    }

    @Override
    public List<Zxicon> geticon() {
        return zx_roleMapper.geticon();
    }
}
